{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.5 Perfect hashing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11.5-1 $\\star$\n",
    "\n",
    "> Suppose that we insert $n$ keys into a hash table of size $m$ using open addressing and uniform hashing. Let $p(n, m)$ be the probability that no collisions occur. Show that $p(n, m) \\le e^{-n(n-1)/2m}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "p(n, m) = \\frac{m}{m} \\cdot \\frac{m-1}{m} \\cdots \\frac{m-n+1}{m} = \\frac{m \\cdot (m-1) \\cdots (m-n+1)}{m^n}\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\begin{array}{rll}\n",
    "\\displaystyle (m - i) \\cdot (m - n + i) &=&\n",
    "\\displaystyle \\left (m - \\frac{n}{2} + \\frac{n}{2} - i \\right ) \\cdot \\left ( m - \\frac{n}{2} - \\frac{n}{2} + i \\right ) \\\\\n",
    "&=& \\displaystyle \\left ( m - \\frac{n}{2} \\right ) ^2 - \\left ( i - \\frac{n}{2} \\right ) ^2 \\\\\n",
    "&\\le& \\displaystyle \\left ( m - \\frac{n}{2} \\right ) ^2\n",
    "\\end{array}\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\begin{array}{rll}\n",
    "p(n, m) &\\le&\n",
    "\\displaystyle \\frac{\\displaystyle m \\cdot \\left ( m - \\frac{n}{2} \\right ) ^ {n-1}}{m^n} \\\\\n",
    "&=& \\displaystyle \\left ( 1 - \\frac{n}{2m} \\right ) ^ {n - 1} \\\\\n",
    "\\end{array}\n",
    "$$\n",
    "\n",
    "Based on equation (3.12), $e^x \\ge 1 + x$,\n",
    "\n",
    "$$\n",
    "\\begin{array}{rll}\n",
    "p(n, m) &\\le&\n",
    "\\displaystyle \\left ( e^{-n/2m} \\right ) ^ {n - 1} \\\\\n",
    "&=& \\displaystyle e^{-n(n-1)/2m}\n",
    "\\end{array}\n",
    "$$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
